package cn.lijiancn01.demo;

import cn.hutool.json.JSONUtil;
import cn.lijiancn01.demo.system.codegen.GenResult;
import cn.lijiancn01.demo.system.codegen.MyCustomConfig;
import cn.lijiancn01.demo.system.codegen.MyCodegenEngine;
import cn.lijiancn01.demo.system.codegen.MyFreemarkerTemplate;
import cn.lijiancn01.demo.system.codegen.MyJavaFileGenerator;
import cn.lijiancn01.demo.system.codegen.MyTemplate;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Lists;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

public class MyFlexCodegenEngineTest {
    //模板类型
    public static final MyTemplate myTemplate = new MyFreemarkerTemplate();

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/amis?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        //通过 datasource 和 globalConfig 创建代码生成器
        GlobalConfig globalConfig = createGlobalConfig(initMyCustomerConfigList());
        MyCodegenEngine generator = new MyCodegenEngine(dataSource, globalConfig);
        if (generator == null) {
            return;
        }
        //生成代码
        HashBasedTable<String, String, GenResult> hashBasedTable = generator.generate();
        System.out.println(JSONUtil.toJsonStr(hashBasedTable));
    }

    public static List<MyCustomConfig> initMyCustomerConfigList() {
        MyCustomConfig config = new MyCustomConfig()
                .setCode("test")
                .setPackagePath("controller")
                .setClassPrefix("Pre")
                .setClassSuffix("Controller")
                .setMyTemplate(myTemplate)
                .setGenerator(new MyJavaFileGenerator("test"))
                .setContent("""
                        <? xml version = "1.0" encoding = "UTF-8" ?>
                        < !DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
                        <mapper namespace="xxx.${table.getEntityJavaFileName()}">
                            <!-- 通用查询映射结果 -->
                            <resultMap id="BaseResultMap" type="xxx.${table.getEntityJavaFileName()}">
                        <#list table.columns as field>
                            <#if field.primaryKey><#--生成主键排在第一位-->
                                <id column="${field.name}" property="${field.property}" />
                            <#else>
                                <result column="${field.name}" property="${field.property}" />
                            </#if>
                        </#list>
                            </resultMap >

                            < !--通用查询结果列 -->
                            <sql id="Base_Column_List">
                        <#list table.columns as field>
                                ${field.name}<#if field_has_next>,</#if>
                        </#list>
                            </sql >
                        </mapper >
                                                """);

        return Lists.newArrayList(config);
    }

    public static GlobalConfig createGlobalConfig(List<MyCustomConfig> myCustomConfigList) {
        if (CollectionUtils.isEmpty(myCustomConfigList)) {
            return null;
        }
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        for (MyCustomConfig myCustomConfig : myCustomConfigList) {
            globalConfig.setCustomConfig(myCustomConfig.getCode(), myCustomConfig);
        }
        //设置 JavadocConfig
        globalConfig.getJavadocConfig()
                .setAuthor("jian.li");

        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("cn.lijiancn01.demo.system")
        ;

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix("gen_")
                .setGenerateTable("gen_template", "gen_datasource", "gen_template_group")
                .setIgnoreColumns("id", "remark", "creator", "create_time", "updater", "update_time", "deleted", "deleted_time")//数据库字段名
        ;
        return globalConfig;
    }
}
